import React, {Component} from 'react';
import TheQueryForm from '@/components/TheQueryForm';
import TheTable from '@/components/TheTable';
import TheTableExtend from '@/components/TheTableExtend';
import ThePagination from '@/components/ThePagination';
import GoBack from '@/components/TheGoBack'
import { apiPostTimedTaskResult } from '@/request/couponManagement';
import store from "@/store/store";
import moment from 'moment'

class TimedTask extends Component {

    constructor(props) {
        super(props)
        this.queryFormRef = React.createRef()
        this.state = {
            isShowQueryForm: true,
            tableSource: [],
            total: 50,
            couponTimerCenterId:this.props.match.params.id,
            columns: [{
                title: '券id',
                dataIndex: 'couponId',
                key: 'couponId',
            }, {
                title: '券名称',
                dataIndex: 'couponTitle',
                key: 'couponTitle',
            }, {
                title: '用户id',
                dataIndex: 'userId',
                key: 'userId',
            }, {
                title: '用户手机号',
                dataIndex: 'memberMobile',
                key: 'memberMobile',
            }, {
                title: '身份',
                dataIndex: 'memberType',
                key: 'memberType',
                render: (text, render) => {
                    let label = ''
                    switch (text) {
                      case 'R':
                        label = '注册会员'
                        break;
                      case "P":
                        label = '正式会员'
                        break;
                      case "S":
                        label = "商户会员"
                        break;
                      default:
                        label = "返回值错误"
                    }
                    return label;
                  }
            }, {
                title: '状态',
                dataIndex: 'memberStatus',
                key: 'memberStatus',
                render: text => {
                    let label = ''
                    switch (text) {
                      case 'Y':
                        label = '正常'
                        break
                      case 'N':
                        label = '冻结'
                        break
                      default:
                        label = '返回值错误'
                        break
                    }
                    return label;
                }
            }, {
                title: '注册时间',
                dataIndex: 'memberCreatedTimeString',
                key: 'memberCreatedTimeString',
               
            },{
                title: '状态',
                dataIndex: 'pushStatus',
                key: 'pushStatus',
                render: text => {
                    let label = ''
                    switch (text) {
                      case 0:
                        label = '发送成功'
                        break
                      case 1:
                        label = '发送失败'
                        break
                      default:
                        label = '返回值错误'
                        break
                    }
                    return label;
                }
            }],
            filterColumns: [],
            queryFormOptions: [
                { field: 'couponId', label: '券id', placeholder: '请输入', require: false, rules: [{pattern: /^[1-9]*[1-9][0-9]*$/, message:'只允许输入非0开始的数字'}], type: 'Input', defaultValue: '' },
                { field: 'userId', label: '用户id', placeholder: '请输入', require: false, rules: [], type: 'InputNumber', defaultValue: '' },
                { field: 'memberMobile', label: '用户手机号', placeholder: '请输入', require: false, rules: [], type: 'InputNumber', defaultValue: '' },
                { field: 'pushStatus', label: '状态', placeholder: '状态', require: false, rules: [], type: 'Select', defaultValue: [], options: [{
                    label: '发送成功',
                    value: 0,
                }, {
                    label: '发送失败',
                    value: 1,
                }]},
            ],
            queryFormFieldsValue: {},
            status: {
                0: '未开始',
                1: '进行中',
                2: '已成功',
            },
            pageData:{
                pageNo: 1,
                pageSize: 10,
            }
            
        }
    }

    componentDidMount() {
        this.setState({
            filterColumns: this.state.columns,
        })
        let searchParams = store.getState().searchValue
        let { pageNo, pageSize, queryFormFieldsValue, pathName } = searchParams
        if (searchParams && pageNo && pathName === 'result' ) {
            let { pageNo, pageSize, code } = searchParams
            this.setState({
                code,
                pageData: {
                    pageNo: pageNo, pageSize: pageSize
                }
            }, () => {
                this.queryFormRef.onSetDefault({ ...queryFormFieldsValue })
                this.getTimedTaskList()
            })
        } else {
            this.getTimedTaskList()
        }
    }   

    onReset() {
        this.setState({
            queryFormFieldsValue:{}
        })
        this.onPageChange(1, 10)
    }

    onSearch(fieldsValue) {
        this.setState({
            pageData: {pageNo:1,pageSize:10}
        },()=>{
            this.getTimedTaskList(fieldsValue)
        })
    }

    onPageChange(pageNo, pageSize) {
        this.setState({
            pageData:{
                pageNo,
                pageSize,
            }
        },()=> {
            this.getTimedTaskList()
        })
    }

    async getTimedTaskList(fieldsValue) {
        const { pageNo, pageSize } = this.state.pageData
        const {couponTimerCenterId} = this.state
        this.setState({
            queryFormFieldsValue: fieldsValue === undefined ? this.state.queryFormFieldsValue : { ...fieldsValue }
        },async () =>{
            const params = {
                pageNo, pageSize, queryFormFieldsValue: this.state.queryFormFieldsValue
            }
            store.dispatch({
                type: 'SaveSearchValues',
                data: {
                    pathName: 'result',
                    ...params
                }
            })
            let res = await apiPostTimedTaskResult({
                ...this.state.queryFormFieldsValue,
                couponTimerCenterId,
                pageNo,
                pageSize,
            })
            if(res.code === 0){
                this.setState({
                    tableSource:res.data,
                    pageData:{
                        pageNo:res.pageNo,
                        pageSize:res.pageSize
                    },
                    total:res.totalCount
                })
            }  
        })

         
    }

    render() { 
        const { tableSource ,isShowQueryForm, queryFormOptions, filterColumns, columns, pageData:{pageNo, pageSize}, total } = this.state
        return (
            <div class="p-layout-box">
                <GoBack history={this.props.history} title="查看" />
                <div class="p-layout-content">
                    <TheQueryForm 
                        ref={(ref) => this.queryFormRef = ref} 
                        show={isShowQueryForm}
                        option={queryFormOptions}
                        onReset={() => this.onReset()}
                        onGetValues={(fieldsValue) => this.onSearch(fieldsValue)}
                    />
                    <TheTableExtend
                        columns={columns}
                        onFormTrigger={() => this.setState({
                            isShowQueryForm: !this.state.isShowQueryForm
                        })}
                        onTableRefresh={() => this.getTimedTaskList()}
                        onColumnsFilter={(value) => this.setState({
                            filterColumns: value
                        })}
                    />
                    <TheTable
                        scroll={{ x: 'max-content', y: '63vh' }} 
                        dataSource={tableSource} 
                        columns={filterColumns}
                    />
                    <ThePagination 
                        pageNo={pageNo}
                        pageSize={pageSize}
                        total={total}
                        pageChange={(pageNo, pageSize) => this.onPageChange(pageNo, pageSize)}
                    />
                </div>
            </div>
        );
    }
}

export default TimedTask;
